package com.atguigu.yygh.user.controller.user;

import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.JwtHelper;
import com.atguigu.yygh.enums.AuthStatusEnum;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user/info")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){
        Map<String,Object> resultMap = userInfoService.login(loginVo);
        return R.ok().data(resultMap);
    }


    //获取用户登录信息
    @GetMapping("/detail")
    public R getUserInfo(@RequestHeader String token){
        Long userId = JwtHelper.getUserId(token);
        UserInfo userInfo = userInfoService.getById(userId);
        userInfo.getParam().put("authStatusString",AuthStatusEnum.getStatusNameByStatus(userInfo.getAuthStatus()));
        return R.ok().data("item",userInfo);
    }

    //跟新用户信息
    @PutMapping("/update")
    public R update(@RequestBody UserAuthVo userAuthVo,@RequestHeader String token){
        Long userId = JwtHelper.getUserId(token);
        userInfoService.updateAuthVo(userId,userAuthVo);
        return R.ok();
    }
}
